(function (window, undefined) {
    var tagReg = new RegExp(/<(\w+)>/);
    this.gm = function (selector) {
        if (window == this) return new gm(selector);
        var doms, domWrapper = [];
        if (tagReg.test(selector)) {
            doms = document.createElement(selector.match(tagReg)[1]);
            domWrapper.push(doms);
        } else{
            doms = document.querySelectorAll(selector);
            for (var i = 0; i < doms.length; i++) {
                domWrapper.push(doms.item(i));
            }
        }
        return this.setArray(domWrapper);
    }
    var MyQuery = this.gm;
    window.gm = this.gm;
    MyQuery.fn = MyQuery.prototype;
    MyQuery.prototype.setArray = function (domWrapper) {
        this.length = 0;
        [].push.apply(this, domWrapper);
        return this;
    }
    //插件扩展1) each 
    MyQuery.fn.each = function (method) {
        for (var i = 0, l = this.length; i < l; i++) {
            method.call(this[i], i);
        }
    }
    //插件扩展2) show 
    MyQuery.fn.show = function () {
        var self = this;
        self.each(function (i) {
            this.style.display = "block";
        });
        return self;
    }
    //插件扩展3) hide 
    MyQuery.fn.hide = function () {
        var self = this;
        self.each(function (i) {
            this.style.display = "none";
        });
        return self;
    }
    //插件扩展4) html 
    MyQuery.fn.html = function (content) {
		if (content != undefined) {
		    this[0].innerHTML = content;
		    return this;
		} else {
		    return this[0].value;
		}
    }
    //插件扩展5) val 
    MyQuery.fn.val = function (content) {
        if (content != undefined) {
            this[0].value = content;
            return this;
        } else {
            return this[0].value;
        }
    }
    //插件扩展6) bind 
    MyQuery.fn.bind = function (eventType, callback) {
        var self = this;
        // 默认只支持一个event参数
        self.each(function (i) {
            this.addEventListener(eventType, function (e) {
                if (callback.arguments) {
                    callback.call(self, e);
                } else {
                    callback.call(self);
                }
            });
        });
        return self;
    }
    //插件扩展7)on 
    MyQuery.fn.on = function (eventType, callback) {
        var self = this;
        // 默认只支持一个event参数
        self.each(function (i) {
            this.addEventListener(eventType, function (event) {
                if (callback.arguments) {
                    callback.call(self, e);
                } else {
                    callback.call(self);
                }
            });
        });
        return self;
    }
    //插件扩展8) keydown
    MyQuery.fn.keydown = function (callback) {
        var self = this;
        // 默认只支持一个event参数
        self.each(function (i) {
            this.addEventListener("onkeydown", function (event) {
                if (callback.arguments) {
                    callback.call(self, e);
                } else {
                    callback.call(self);
                }
            });
        });
        return self;
    }
    //插件扩展9) keydown
    MyQuery.fn.css = function (paramObj) {
        var self = this;
        if (typeof(paramObj)=='string') {
            return this[0].style[paramObj];
        }
        // 默认只支持一个event参数
        self.each(function (i) {
            for(var key in paramObj){
                this.style[key] = paramObj[key];
            }
        });
        return self;
    }
    //插件扩展10) keydown
    MyQuery.fn.attr = function (paramObj) {
        var self = this;
        if (typeof(paramObj)=='string') {
            return this[0].getAttribute(paramObj);
        }
        // 默认只支持一个event参数
        self.each(function (i) {
            for(var key in paramObj){
                this.setAttribute(key, paramObj[key]);
            }
        });
        return self;
    }
    //插件扩展11) append
    MyQuery.fn.append = function (gmDom) {
        var self = this;
        // 默认只支持一个event参数
        self.each(function (i) {
            if (gmDom instanceof HTMLElement) {
                self[0].appendChild(gmDom);
            }else{
                self[0].appendChild(gmDom[0]);
            }
        });
        return self;
    }
     //插件扩展12) hasClass
     MyQuery.fn.hasClass = function (class_name) {
        var targetDom = this[0];
        var classArray = targetDom.className.split(" ");
        for(var i in classArray){
            if (classArray[i] === class_name) {
                return true;
            }
        }
        return false;
    }
})(window);